<?php
    require_once("core/functions.php");
    core::getResource("Controller.LoginController");
    session_start();
?>
<?php $title = "Event Report"; ?>
<?php include_once("header.php"); ?>
<div class="centerContent">
    <div id="loader" style="z-index:10; position: absolute; left: 50%"></div>
    <form id="reportTable" class="reportTable" style="display:none; border: solid 2px blue;">
        <table>
            <tr>
                <td><h1>Event</h1></td>
                <td></td>
            </tr>
            <tr>
                <td colspan="2"><hr/></td>
            </tr>
            <tr>
                <td>Event Name:</td>
                <td><a title="View Event">
                        <input type="hidden" name="name"/>
                    </a>
                </td>
            </tr>
            <tr>
                <td>Event Description:</td>
                <td><input type="hidden" name="description"/></td>
            </tr>
            <tr>
                <td>Start Time:</td>
                <td><input type="hidden" name="startTime"/></td>
            </tr>
            <tr>
                <td>End Time:</td>
                <td><input type="hidden" name="endTime"/></td>
            </tr>
            <tr>
                <td>Start Date:</td>
                <td><input type="hidden" name="startDate"/></td>
            </tr>
            <tr>
                <td>End Date:</td>
                <td><input type="hidden" name="endDate"/></td>
            </tr>
            <tr>
                <td>Location Details:</td>
                <td><input type="hidden" name="locationDetails"/></td>
            </tr>
            <tr>
                <td>Car Pool Location:</td>
                <td><input type="hidden" name="carPoolLocation"/></td>
            </tr>
            <tr>
                <td>Car Pool Time:</td>
                <td><input type="text" class="inputLabel" name="carPoolTime"/></td>
            </tr>
            <tr>
                <td>Meeting Location:</td>
                <td><input type="hidden" name="meetingLocation"/></td>
            </tr>
            <tr>
                <td>Meeting Time:</td>
                <td><input type="hidden" name="meetingTime"/></td>
            </tr>
            <tr>
                <td>Date Created:</td>
                <td><input type="hidden" name="dateCreated"/></td>
            </tr>
            <tr>
                <td colspan="2" id="activityContainer"></td>
            </tr>
        </table>
    </form>
    <form class="hidden" id="activityTemplate">
        <table style="border: solid 2px red">
            <tr>
                <td><h1>Activity</h1></td>
                <td></td>
            </tr>
            <tr>
                <td colspan="2"><hr/></td>
            </tr>
            <tr>
                <td>Activity Title:</td>
                <td><a title="View Activity">
                        <input type="hidden" name="title"/>
                    </a>
                </td>
            </tr>
            <tr>
                <td>Activity Description:</td>
                <td><input type="hidden" name="description"/></td>
            </tr>
            <tr>
                <td>Start Time:</td>
                <td><input type="hidden" name="startTime"/></td>
            </tr>
            <tr>
                <td>End Time:</td>
                <td><input type="hidden" name="endTime"/></td>
            </tr>
            <tr>
                <td>Start Date:</td>
                <td><input type="hidden" name="startDate"/></td>
            </tr>
            <tr>
                <td>End Date:</td>
                <td><input type="hidden" name="endDate"/></td>
            </tr>
            <tr>
                <td>Date Created:</td>
                <td><input type="hidden" name="dateCreated"/></td>
            </tr>
            <tr>
                <td colspan="2" data-id="roleContainer"></td>
            </tr>
        </table>
    </form>
    <form class="hidden" id="roleTemplate">
        <table style="border: solid 2px green">
            <tr>
                <td><h1>Role</h1></td>
                <td></td>
            </tr>
            <tr>
                <td colspan="2"><hr/></td>
            </tr>
            <tr>
                <td>Role Title:</td>
                <td><a title="View Role">
                        <input type="hidden" name="title"/>
                    </a>
                </td>
            </tr>
            <tr>
                <td>Role Description:</td>
                <td><input type="hidden" name="description"/></td>
            </tr>
            <tr>
                <td>Start Time:</td>
                <td><input type="hidden" name="startTime"/></td>
            </tr>
            <tr>
                <td>End Time:</td>
                <td><input type="hidden" name="endTime"/></td>
            </tr>
            <tr>
                <td>Start Date:</td>
                <td><input type="hidden" name="startDate"/></td>
            </tr>
            <tr>
                <td>End Date:</td>
                <td><input type="hidden" name="endDate"/></td>
            </tr>
            <tr>
                <td>Positions:</td>
                <td><input type="hidden" name="positions"/></td>
            </tr>
            <tr>
                <td>Pay Rate: $</td>
                <td><input type="hidden" name="payRate"/></td>
            </tr>
            <tr>
                <td>Skills Required:</td>
                <td><input type="hidden" name="skills"/></td>
            </tr>
            <tr>
                <td>Date Created:</td>
                <td><input type="hidden" name="dateCreated"/></td>
            </tr>
            <tr>
                <td colspan="2" data-id="applicantContainer"></td>
            </tr>
        </table>
    </form>
    <form class="hidden" id="applicantTemplate">
        <table style="border: solid 2px orange">
            <tr>
                <td><h1>Applicant</h1></td>
                <td></td>
            </tr>
            <tr>
                <td colspan="2"><hr/></td>
            </tr>
            <tr>
                <td>Applicant Name:</td>
                <td><a title="View Profile">
                        <input type="hidden" name="name"/>
                    </a>
                </td>
            </tr>
            <tr>
                <td>Attended:</td>
                <td><input type="hidden" name="attended"/></td>
            </tr>
            <tr>
                <td>Hours Worked:</td>
                <td><input type="hidden" name="hoursWorked"/></td>
            </tr>
            <tr>
                <td>Application Date:</td>
                <td><input type="hidden" name="dateCreated"/></td>
            </tr>
        </table>
    </form>
</div>
<script>
    var report = (function() {
        var param = UTSDK.getUrlParam("report");
        if (!param)
            window.location = "index.php";
        var form = UTSDK.$("reportTable");
        var loader = UTSDK.$("loader");
        var hr = "<hr/>";
        loader.innerHTML = "<img src='images/ajax-loader.gif'/>";
        var json = {report: param};

        window.addEventListener("load", function() {
            UTSDK.AJAX.get("update.php", json, function(responce) {
                responce = JSON.parse(responce);
                populateTable(responce);
                finished();
            }, true);
        });

        function finished() {
            UTSDK.fadeOut(loader, 20);
            UTSDK.fadeIn(form, 20);
        }

        function populateTable(responce) {
            var activitytemp = UTSDK.$("activityTemplate");
            var roletemp = UTSDK.$("roleTemplate");
            var applicanttemp = UTSDK.$("applicantTemplate");

            var eActivity = UTSDK.$("activityContainer");
            var aRole;
            var rApplicant;

            var event = responce["event"];
            for (var x in event) {
                if (form.elements[x]) {
                    if (x === "name") {
                        form.elements[x].parentElement.href = "viewevent.php?eventId=" + event["eventId"];
                    }
                    UTSDK.setInnerText(form.elements[x].parentElement, event[x]);
                    //UTSDK.dispatchEvent(form.elements[x], "change");
                }
                if (x === "activities") {
                    var activities = event["activities"];
                    for (var i = 0; i < activities.length; i++) {
                        var aTemp = activitytemp.cloneNode(true);
                        aRole = UTSDK.getChildByDataId(aTemp, "roleContainer");
                        UTSDK.removeClass(aTemp, "hidden");
                        aTemp.id = "activity" + i;
                        for (var y in activities[i]) {
                            if (aTemp.elements[y]) {
                                var elem = aTemp.elements[y].parentElement;
                                if (y === "title") {
                                    elem.href = "viewactivity.php?activityId=" + activities[i]["activityId"];
                                }
                                UTSDK.setInnerText(elem, activities[i][y]);
                            }
                            if (y === "roles") {
                                var roles = activities[i]["roles"];
                                for (var j = 0; j < roles.length; j++) {
                                    var rTemp = roletemp.cloneNode(true);
                                    rApplicant = UTSDK.getChildByDataId(rTemp, "applicantContainer");
                                    UTSDK.removeClass(rTemp, "hidden");
                                    rTemp.id = "role" + j;
                                    for (var z in roles[j]) {
                                        if (rTemp.elements[z]) {
                                            var elem = rTemp.elements[z].parentElement;
                                            if (z === "title") {
                                                elem.href = "viewrole.php?roleId=" + roles[j]["roleId"];
                                            }
                                            UTSDK.setInnerText(elem, roles[j][z]);
                                            if (z === "skills") {
                                                skillsChange(elem);
                                            }
                                        }
                                        if (z === "applied") {
                                            var applicants = roles[j]["applied"];
                                            for (var k = 0; k < applicants.length; k++) {
                                                var pTemp = applicanttemp.cloneNode(true);
                                                UTSDK.removeClass(pTemp, "hidden");
                                                pTemp.id = "applicant" + k;
                                                for (var a in applicants[k]) {
                                                    if (pTemp.elements[a]) {
                                                        var elem = pTemp.elements[a].parentElement;
                                                        if (a === "name") {
                                                            elem.href = "viewprofile.php?person=" + applicants[k]["person"];
                                                        }
                                                        UTSDK.setInnerText(elem, applicants[k][a]);
                                                        if (a === "attended") {
                                                            attendedChange(elem);
                                                        }
                                                    }
                                                }
                                                rApplicant.appendChild(pTemp.children[0]);
                                            }
                                        }
                                    }
                                    aRole.appendChild(rTemp.children[0]);
                                }
                            }
                        }
                        eActivity.appendChild(aTemp.children[0]);
                    }
                }
            }
        }
    })();

    function attendedChange(elem) {
        if ("textContent" in elem) {
            if (elem.textContent === "")
                elem.textContent = "Not specified";
        }
        else {
            if (elem.innerText === "")
                elem.innerText = "Not specified";
        }
    }

    function skillsChange(elem) {
        attendedChange(elem);
    }
</script>
<?php include_once("footer.php"); ?>